﻿using Furion;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SqlSugar;
using System;
using System.Collections.Generic;
using Thor.Core;

namespace Thor.Web.Core
{
    public class Startup: AppStartup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            SqlSugarConfigure(services);

            services.AddCorsAccessor();

            services.AddControllers()
                .AddMvcFilter<TransactionalFilter>()
                .AddInjectWithUnifyResult();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection(); // 强制https

            app.UseStaticFiles();

            app.UseRouting();

            app.UseCorsAccessor();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseInject(string.Empty);

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

        }

        private void SqlSugarConfigure(IServiceCollection services)
        {
            #region 配置sqlsuagr
            //默认数据库为master
            var connectOptions = App.GetConfig<List<ConnectionConfig>>("DbConfig");
            services.AddSqlSugar(connectOptions.ToArray()
                , db =>
                {
                    db.Aop.OnLogExecuting = (sql, pars) =>
                    {
                        if (sql.StartsWith("SELECT"))
                        {
                            Console.ForegroundColor = ConsoleColor.Green;
                        }
                        if (sql.StartsWith("UPDATE") || sql.StartsWith("INSERT"))
                        {
                            Console.ForegroundColor = ConsoleColor.White;
                        }
                        if (sql.StartsWith("DELETE"))
                        {
                            Console.ForegroundColor = ConsoleColor.Blue;
                        }
                        Console.WriteLine(sql + "\r\n\r\n" + SqlProfiler.ParameterFormat(sql, pars));
                        App.PrintToMiniProfiler("SqlSugar", "Info", SqlProfiler.ParameterFormat(sql, pars));
                    };
                    //执行超时时间
                    db.Ado.CommandTimeOut = 30;

                    // 配置加删除全局过滤器
                    //db.QueryFilter.Add(new TableFilterItem<SysApp>(it => it.IsDeleted == false));
                    //db.QueryFilter.Add(new TableFilterItem<SysCodeGen>(it => it.IsDeleted == false));
                    //db.QueryFilter.Add(new TableFilterItem<SysCodeGenConfig>(it => it.IsDeleted == false));
                    //db.QueryFilter.Add(new TableFilterItem<SysDictData>(it => it.IsDeleted == false));
                    //db.QueryFilter.Add(new TableFilterItem<SysDictType>(it => it.IsDeleted == false));
                    //db.QueryFilter.Add(new TableFilterItem<SysFile>(it => it.IsDeleted == false));
                    //db.QueryFilter.Add(new TableFilterItem<SysMenu>(it => it.IsDeleted == false));
                    //db.QueryFilter.Add(new TableFilterItem<SysNotice>(it => it.IsDeleted == false));
                    //db.QueryFilter.Add(new TableFilterItem<SysOauthUser>(it => it.IsDeleted == false));
                    //db.QueryFilter.Add(new TableFilterItem<SysOrg>(it => it.IsDeleted == false));
                    //db.QueryFilter.Add(new TableFilterItem<SysPos>(it => it.IsDeleted == false));
                    //db.QueryFilter.Add(new TableFilterItem<SysRole>(it => it.IsDeleted == false));
                    //db.QueryFilter.Add(new TableFilterItem<SysTimer>(it => it.IsDeleted == false));
                    //db.QueryFilter.Add(new TableFilterItem<SysUser>(it => it.IsDeleted == false));
                    //db.QueryFilter.Add(new TableFilterItem<SysTenant>(it => it.IsDeleted == false));
                });
            #endregion
        }
    }
}